home *** CD-ROM | disk | FTP | other *** search
/ Java Interactive Reference Guide / Java Interactive Reference Guide.iso / autorun / source.dir / 00060_15.txt < prev    next >
Encoding:
Text File  |  1980-01-11  |  9.3 KB  |  332 lines

  1. /*
  2.  * @(#)GraphicsTest.java    1.3 95/09/01 Sami Shaio
  3.  *
  4.  * Copyright (c) 1994-1995 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Permission to use, copy, modify, and distribute this software
  7.  * and its documentation for NON-COMMERCIAL or COMMERCIAL purposes and
  8.  * without fee is hereby granted. 
  9.  * Please refer to the file http://java.sun.com/copy_trademarks.html
  10.  * for further important copyright and trademark information and to
  11.  * http://java.sun.com/licensing.html for further important licensing
  12.  * information for the Java (tm) Technology.
  13.  * 
  14.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  15.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  16.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  17.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  18.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  19.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  20.  * 
  21.  * THIS SOFTWARE IS NOT DESIGNED OR INTENDED FOR USE OR RESALE AS ON-LINE
  22.  * CONTROL EQUIPMENT IN HAZARDOUS ENVIRONMENTS REQUIRING FAIL-SAFE
  23.  * PERFORMANCE, SUCH AS IN THE OPERATION OF NUCLEAR FACILITIES, AIRCRAFT
  24.  * NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL, DIRECT LIFE
  25.  * SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH THE FAILURE OF THE
  26.  * SOFTWARE COULD LEAD DIRECTLY TO DEATH, PERSONAL INJURY, OR SEVERE
  27.  * PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH RISK ACTIVITIES").  SUN
  28.  * SPECIFICALLY DISCLAIMS ANY EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR
  29.  * HIGH RISK ACTIVITIES.
  30.  */
  31. import java.awt.*;
  32. import java.applet.*;
  33.  
  34. public class GraphicsTest extends Applet {
  35.     GraphicsCards    cards;
  36.     public void init() {
  37.     setLayout(new BorderLayout());
  38.     add("Center", cards = new GraphicsCards());
  39.     Panel p = new Panel();
  40.     p.add(new Button("next"));
  41.     p.add(new Button("previous"));
  42.     p.add(new Label("go to:", Label.RIGHT));
  43.  
  44.     Choice c;
  45.  
  46.     p.add(c = new Choice());
  47.     c.addItem("Arc");
  48.     c.addItem("Oval");
  49.     c.addItem("Polygon");
  50.     c.addItem("Rect");
  51.     c.addItem("RoundRect");
  52.     add("North", p);
  53.     }
  54.     public boolean action(Event evt, Object arg) {
  55.     if (evt.target instanceof Choice) {
  56.         ((CardLayout)cards.getLayout()).show(cards,(String)arg);
  57.     } else {
  58.         if ("next".equals(arg)) {
  59.         ((CardLayout)cards.getLayout()).next(cards);
  60.         } else if ("previous".equals(arg)) {
  61.         ((CardLayout)cards.getLayout()).previous(cards);
  62.         }
  63.     }
  64.  
  65.     return true;
  66.     }
  67.  
  68.     public static void main(String args[]) {
  69.     GraphicsFrame f = new GraphicsFrame();
  70.     GraphicsTest graphicsTest = new GraphicsTest();
  71.  
  72.     graphicsTest.init();
  73.     f.add("Center", graphicsTest);
  74.     f.resize(300, 300);
  75.     f.show();
  76.     }
  77. }
  78.  
  79. class GraphicsFrame extends Frame {
  80.     public GraphicsFrame() {
  81.     super("Graphics Test");
  82.     }
  83.     public boolean handleEvent(Event e) {
  84.     if (e.id == Event.WINDOW_DESTROY) {
  85.         System.exit(0);
  86.     }
  87.     return false;
  88.     }
  89. }
  90.     
  91. class GraphicsCards extends Panel {
  92.     public GraphicsCards() {
  93.     setLayout(new CardLayout());
  94.     add("Arc", new ArcCard());
  95.     add("Oval", new ShapeTest(new OvalShape()));
  96.     add("Polygon", new ShapeTest(new PolygonShape()));
  97.     add("Rect", new ShapeTest(new RectShape()));
  98.     add("RoundRect", new ShapeTest(new RoundRectShape()));
  99.     }
  100. }
  101.  
  102.  
  103. class ArcCard extends Panel {
  104.     public ArcCard() {
  105.     setLayout(new GridLayout(0, 2));
  106.     add(new ArcPanel(true));
  107.     add(new ArcPanel(false));
  108.     add(new ArcDegreePanel(true));
  109.     add(new ArcDegreePanel(false));
  110.     }
  111. }
  112.  
  113. class ArcDegreePanel extends Panel {
  114.     boolean filled;
  115.     public ArcDegreePanel(boolean filled) {
  116.     this.filled = filled;
  117.     }
  118.     void arcSteps(Graphics g,
  119.           int step, int x, int y, int w, int h,
  120.           Color c1,
  121.           Color c2) {
  122.     int a1 = 0;
  123.     int a2 = step;
  124.     int progress = 0;
  125.     g.setColor(c1);
  126.     for (; (a1+a2) <= 360; a1 = a1+a2, a2 += 1 ) {
  127.         if (g.getColor() == c1) {
  128.         g.setColor(c2);
  129.         } else {
  130.         g.setColor(c1);
  131.         }
  132.         if (filled) {
  133.         g.fillArc(x, y, w, h, a1, a2);
  134.         } else {
  135.         g.drawArc(x, y, w, h, a1, a2);
  136.         }
  137.         progress = a1+a2;
  138.     }
  139.     if (progress != 360) {
  140.         if (filled) {
  141.         g.fillArc(x, y, w, h, a1, 360 - progress);
  142.         } else {
  143.         g.drawArc(x, y, w, h, a1, 360 - progress);
  144.         }
  145.     }
  146.     }
  147.     public void paint(Graphics g) {
  148.     Rectangle r = bounds();
  149.  
  150.     arcSteps(g, 3, 0, 0, r.width, r.height,
  151.          Color.orange, Color.blue);
  152.     arcSteps(g, 2, r.width / 4, r.height / 4, r.width / 2, r.height / 2,
  153.          Color.yellow, Color.green);
  154.     arcSteps(g, 1,
  155.          (r.width  * 3) / 8,
  156.          (r.height * 3) / 8,
  157.          r.width / 4, r.height / 4,
  158.          Color.magenta, Color.white);
  159.     }
  160. }
  161.  
  162. class ArcPanel extends Panel {
  163.     boolean filled;
  164.     public ArcPanel(boolean filled) {
  165.     this.filled = filled;
  166.     }
  167.     public void paint(Graphics g) {
  168.     Rectangle r = bounds();
  169.     g.setColor(Color.yellow);
  170.     if (filled) {
  171.         g.fillArc(0, 0, r.width, r.height, 0, 45);
  172.     } else {
  173.         g.drawArc(0, 0, r.width, r.height, 0, 45);
  174.     }
  175.     g.setColor(Color.green);
  176.     if (filled) {
  177.         g.fillArc(0, 0, r.width, r.height, 90, -45);
  178.     } else {
  179.         g.drawArc(0, 0, r.width, r.height, 90, -45);
  180.     }
  181.     g.setColor(Color.orange);
  182.     if (filled) {
  183.         g.fillArc(0, 0, r.width, r.height, 135, -45);
  184.     } else {
  185.         g.drawArc(0, 0, r.width, r.height, 135, -45);
  186.     }
  187.     g.setColor(Color.magenta);
  188.     if (filled) {
  189.         g.fillArc(0, 0, r.width, r.height, -225, 45);
  190.     } else {
  191.         g.drawArc(0, 0, r.width, r.height, -225, 45);
  192.     }
  193.  
  194.     g.setColor(Color.yellow);
  195.     if (filled) {
  196.         g.fillArc(0, 0, r.width, r.height, 225, -45);
  197.     } else {
  198.         g.drawArc(0, 0, r.width, r.height, 225, -45);
  199.     }
  200.     g.setColor(Color.green);
  201.     if (filled) {
  202.         g.fillArc(0, 0, r.width, r.height, -135, 45);
  203.     } else {
  204.         g.drawArc(0, 0, r.width, r.height, -135, 45);
  205.     }
  206.     g.setColor(Color.orange);
  207.     if (filled) {
  208.         g.fillArc(0, 0, r.width, r.height, -45, -45);
  209.     } else {
  210.         g.drawArc(0, 0, r.width, r.height, -45, -45);
  211.     }
  212.     g.setColor(Color.magenta);
  213.     if (filled) {
  214.         g.fillArc(0, 0, r.width, r.height, 315, 45);
  215.     } else {
  216.         g.drawArc(0, 0, r.width, r.height, 315, 45);
  217.     }
  218.     }
  219. }
  220.  
  221. abstract class Shape {
  222.     abstract void draw(Graphics g, int x, int y, int w, int h);
  223.     abstract void fill(Graphics g, int x, int y, int w, int h);
  224. }
  225.  
  226. class RectShape extends Shape {
  227.     void draw(Graphics g, int x, int y, int w, int h) {
  228.     g.drawRect(x, y, w, h);
  229.     }
  230.     void fill(Graphics g, int x, int y, int w, int h) {
  231.     g.fillRect(x, y, w, h);
  232.     }
  233. }
  234.  
  235. class OvalShape extends Shape {
  236.     void draw(Graphics g, int x, int y, int w, int h) {
  237.     g.drawOval(x, y, w, h);
  238.     }
  239.     void fill(Graphics g, int x, int y, int w, int h) {
  240.     g.fillOval(x, y, w, h);
  241.     }
  242. }
  243.  
  244. class RoundRectShape extends Shape {
  245.     void draw(Graphics g, int x, int y, int w, int h) {
  246.     g.drawRoundRect(x, y, w, h, 10, 10);
  247.     }
  248.     void fill(Graphics g, int x, int y, int w, int h) {
  249.     g.fillRoundRect(x, y, w, h, 10, 10);
  250.     }
  251. }
  252.  
  253. class PolygonShape extends Shape {
  254.     Polygon p;
  255.     public PolygonShape() {
  256.     p = new Polygon();
  257.     p.addPoint(0, 0);
  258.     p.addPoint(10, 0);
  259.     p.addPoint(5, 15);
  260.     p.addPoint(10, 20);
  261.     p.addPoint(5, 20);
  262.     p.addPoint(0, 10);
  263.     p.addPoint(0, 0);
  264.     }
  265.     void draw(Graphics g, int x, int y, int w, int h) {
  266.     Graphics ng = g.create();
  267.     ng.translate(x, y);
  268.     ng.scale((float)((float)w / (float)10), (float)((float)h / (float)20));
  269.     ng.drawPolygon(p);
  270.     }
  271.     void fill(Graphics g, int x, int y, int w, int h) {
  272.     Graphics ng = g.create();
  273.     ng.translate(x, y);
  274.     ng.scale((float)((float)w / (float)10), (float)((float)h / (float)20));
  275.     ng.fillPolygon(p);
  276.     }
  277. }
  278.  
  279. class ShapeTest extends Panel {
  280.     Shape    shape;
  281.     int        step;
  282.  
  283.     public ShapeTest(Shape shape, int step) {
  284.     this.shape = shape;
  285.     this.step = step;
  286.     }
  287.  
  288.     public ShapeTest(Shape shape) {
  289.     this(shape, 10);
  290.     }
  291.  
  292.     public void paint(Graphics g) {
  293.     Rectangle bounds = bounds();
  294.  
  295.     int cx, cy, cw, ch;
  296.  
  297.     Color color;
  298.     for (color=Color.red, cx=bounds.x, cy=bounds.y,
  299.          cw=bounds.width / 2, ch=bounds.height; 
  300.          cw > 0 && ch > 0;
  301.          cx+=step, cy += step, cw -= (step * 2), ch -= (step * 2),
  302.          color=ColorUtils.darker(color, 0.9)) {
  303.         g.setColor(color);
  304.         shape.draw(g, cx, cy, cw, ch);
  305.     }
  306.     for (cx=bounds.x + bounds.width / 2, cy=bounds.y,
  307.          cw=bounds.width / 2, ch=bounds.height; 
  308.          cw > 0 && ch > 0;
  309.          cx+=step, cy += step, cw -= (step * 2),ch -= (step * 2)) {
  310.         if (g.getColor() == Color.red) {
  311.         g.setColor(Color.blue);
  312.         } else {
  313.         g.setColor(Color.red);
  314.         }
  315.         shape.fill(g, cx, cy, cw, ch);
  316.     }
  317.     }
  318. }
  319.  
  320. class ColorUtils {
  321.     static Color brighter(Color c, double factor) {
  322.     return new Color(Math.min((int)(c.getRed()  *(1/factor)), 255), 
  323.              Math.min((int)(c.getGreen()*(1/factor)), 255),
  324.              Math.min((int)(c.getBlue() *(1/factor)), 255));
  325.     }
  326.     static Color darker(Color c, double factor) {
  327.     return new Color(Math.max((int)(c.getRed()  *factor), 0), 
  328.              Math.max((int)(c.getGreen()*factor), 0),
  329.              Math.max((int)(c.getBlue() *factor), 0));
  330.     }
  331. }
  332.